import scrapy
from scrapy.loader import ItemLoader
from HBookerScrapy.items import Book


class BookSpider(scrapy.Spider):
    name = 'bookspider'
    start_urls = ['https://www.ciweimao.com/book/100100123']

    def parse(self, response):
        il = ItemLoader(item=Book(), response=response)
        il.add_xpath('title', '//div[@class="book-info"]/h3[@class="title"]/text()')
        il.add_xpath('author', '//h3[@class="title"]/span/a/text()')
        il.add_xpath('cover', '//div[@class="cover ly-fl"]/img/@src')

        il.add_xpath('clickTotal', '//div[@class="book-property clearfix"]/span[2]/i/text()')
        il.add_xpath('clickMonth', '//div[@class="book-property clearfix"]/span[3]/i/text()')
        il.add_xpath('clickWeek', '//div[@class="book-property clearfix"]/span[4]/i/text()')

        il.add_xpath('recommendTotal', '//div[@class="book-property clearfix"]/span[6]/i/text()')
        il.add_xpath('recommendMonth', '//div[@class="book-property clearfix"]/span[7]/i/text()')
        il.add_xpath('recommendWeek', '//div[@class="book-property clearfix"]/span[8]/i/text()')

        il.add_xpath('wordTotal', '//div[@class="book-property clearfix"]/span[9]/i/text()')
        il.add_xpath('monthTicketTotal', '//li[@class="month"]/h3/text()')
        il.add_xpath('collect', '//p[@class="book-grade"]/b[2]/text()')

        return il.load_item()
